<?php defined('BASEPATH') OR exit('No direct script access allowed');
/**
 * Description: 评论
 *
 * @author      Eden
 * @datetime    2016/12/9 00:43
 * @copyright   Beijing CmsTop Technology Co.,Ltd.
 */
class Comment_Model extends API_Model
{
    /**
     * 参数
     * @var array
     */
    private $params = array();

    /**
     * 初始化
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 主函数
     * @param array $params
     */
    public function run(Array $params)
    {
        $this->params = $params;
        $send['state'] = false;
        do {

            if (!$this->_checkParams()) {
                $send['error'] = $this->getError();
                break;
            }

            if ($data = $this->saveComment()) {
                $send['state'] = true;
                $send['data'] = $data;
            } else {
                $send['error'] = '评论失败';
            }
        } while(false);
        $this->output->send($send);
    }

    /**
     * 保存
     * @return bool
     */
    private function saveComment()
    {
        $cDb = clone($this->db);
        $cDb->trans_start();

        $nowDate = dateTime();
        $saveData = array(
            'bbsId' => $this->params['bbsId'],
            'userId' => $this->params['userId'],
            'text' => $this->params['text'],
            'created' => $nowDate,
            'status' => 1
        );
        $cDb->insert('bbs_comments', $saveData);

        $comId = $cDb->insert_id();
        if ($comId) {
            $db = clone($this->db);
            $query = $db->get_where('bbs', array('id' => $this->params['bbsId']));
            $row = $query->row_array();
            if ($row) {
                $comments = intval($row['comments']) + 1;
                $cDb->where('id', $this->params['bbsId']);
                $cDb->update('bbs', array('comments' => $comments));
                if ($cDb->affected_rows()) {
                    $cDb->trans_complete();
                }
            }
        }

        if ($cDb->trans_status() === false) {
            return false;
        }

        $userInfo = $this->_getNameAndAvatar($this->params['userId']);
        return array(
            'commentId' => intval($comId),
            'nickName' => $userInfo['nickName'],
            'avatar' => thumb($userInfo['avatar'], 100, 100),
            'text' => $saveData['text'],
            'created' => formateTimeStamp($saveData['created']),
            'reply' => new stdClass()
        );
    }

    /**
     * 参数检查
     * @return bool
     */
    private function _checkParams()
    {
        if ($this->input->method(true) !== 'POST') {
            $this->setError('非法请求');
            return false;
        }

        if (empty($this->params['userId']) || empty($this->params['bbsId']) || empty($this->params['text'])) {
            $this->setError('缺少必填参数');
            return false;
        }

        // 用户是否存在
        if (!$this->_checkUserExists($this->params['userId'])) {
            $this->setError('用户不存在或被限制使用');
            return false;
        }

        // 帖子是否存在
        if (!$this->_checkBbsExists($this->params['bbsId'])) {
            $this->setError('帖子不存在');
            return false;
        }

        return true;
    }

}